<?php
/**
 * Comments module for XOOPS
 *
 * You may not change or alter any portion of this comment or credits
 * of supporting developers from this source code or any supporting source code 
 * which is considered copyrighted (c) material of the original comment or credit authors.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @copyright       The Groups Project http://code.google.com/p/commentsforxoops/
 * @license         http://www.fsf.org/copyleft/gpl.html GNU public license
 * @package         comments
 * @since           2.3.0
 * @author          Hui Xiao < xh.8326@gmail.com >
 * @version         $Id: comments.php Sat Jul 18 20:19:43 CST 2009
 */

if (false === defined("XOOPS_ROOT_PATH")) {exit();}

class commentsComments extends XoopsObject {
	public function __construct() {
		$this->initVar("com_id", XOBJ_DTYPE_INT, null, false);
		$this->initVar("com_pid", XOBJ_DTYPE_INT);
        $this->initVar("com_rootid", XOBJ_DTYPE_INT);
        $this->initVar("com_modid", XOBJ_DTYPE_INT);
        $this->initVar("com_itemid", XOBJ_DTYPE_INT);
        $this->initVar("com_icon", XOBJ_DTYPE_TXTBOX);
        $this->initVar("com_created", XOBJ_DTYPE_INT);
        $this->initVar("com_modified", XOBJ_DTYPE_INT);
        $this->initVar("com_uid", XOBJ_DTYPE_INT);
        $this->initVar("com_ip", XOBJ_DTYPE_TXTBOX);
        $this->initVar("com_title", XOBJ_DTYPE_TXTBOX);
        $this->initVar("com_text", XOBJ_DTYPE_TXTAREA);
        $this->initVar("com_sig", XOBJ_DTYPE_TXTAREA);
        $this->initVar("com_status", XOBJ_DTYPE_INT);
        $this->initVar("com_exparams", XOBJ_DTYPE_TXTBOX);
	}
	
}

class commentsCommentsHandler extends XoopsPersistableObjectHandler
{
	public function __construct(&$db) {
        parent::__construct($db,"xoopscomments","commentsComments","com_id");
    } 
    
    public function setComment($comid=0, $modid, $itemid, $uid, $title="", $text, $status) {
        if ( empty($modid) || empty($itemid) || empty($uid) || empty($text) ) return false;
        $time = time();
        if ( !empty($comid) ) {
            $obj = $this->get($comid);
            if ( empty($obj) || !is_object($obj) ) return false;
        } else {
            $obj = $this->get();
            $obj->setVar("com_uid",$uid);
            $obj->setVar("com_modid",$modid);
            $obj->setVar("com_itemid",$itemid);
            $obj->setVar("com_created",$time);
            $user_ip = (isset($_SERVER["HTTP_X_FORWARDED_FOR"]))? $_SERVER["HTTP_X_FORWARDED_FOR"] : $_SERVER["REMOTE_ADDR"];
            $obj->setVar("com_ip",$user_ip);
        }
        $obj->setVar("com_title",$title);
        $obj->setVar("com_text",$text);
        $obj->setVar("com_modified",$time);
        $obj->setVar("com_status",$status);
        if ( $com_id = $this->insert($obj) ) {
            return $com_id;
        }
        return false;
    }
    
    public function getCommentsList($module, $itemid, $uid=0,$status=0, $start=0, $limit=10, $order="DESC", $sort="com_id") {
        if ( empty($module) ) return false;
        $module_handler = xoops_gethandler("module");
		$_module_obj = $module_handler->getByDirname($module);
		$extar = "";
        $ret = array();
        $criteria = new CriteriaCompo();
        $criteria->add(new Criteria("com_modid",$_module_obj->getVar("mid")));
        $criteria->add(new Criteria("com_itemid",$itemid));
        $extar .= "id={$itemid}";
        if ( !empty($status) ) {
            $criteria->add(new Criteria("com_status",$status));
        }
        if ( !empty($uid) ) {
            $criteria->add(new Criteria("com_uid",$uid));
        }
        $criteria->setStart($start);
        $criteria->setLimit($limit);
        $criteria->setOrder($order);
        $criteria->setSort($sort);
        $counts = $this->getCount($criteria);
        $ret["pagenav"] = "";
        $ret["list"] = array();
        if( $counts > $limit ){	
        	$nav = new XoopsPageNav($counts, $limit, $start, "start",$extar);
        	$ret["pagenav"] = $nav->renderNav(4);
        }
        $comment_list = $this->getAll($criteria, null, false);
        unset($criteria);
        if ( $comment_list ) {
            $_uids = array();
            foreach ( $comment_list as $k=>$v ) {
                $_uids[] = $v["com_uid"];
            }
            $list = $_user = $_avatar = array();
            if ( $_uids ) {
                $_uids = array_unique($_uids);
                $member_handler = xoops_gethandler("member");
                $criteria = new CriteriaCompo( new Criteria("uid", $_uids, "in"));
                $_user_objs = $member_handler->getUsers($criteria);
                if ( $_user_objs ) {
                    foreach ( $_user_objs as $k=>$obj ) {
                        $_user[$obj->getVar("uid")] = $obj->name();
                        $_avatar[$obj->getVar("uid")] = $obj->user_avatar();
                    }
                }
            }
            foreach ( $comment_list as $k=>$v ) {
                $list[$k] = $v;
                $list[$k]["uid"] = $v["com_uid"];
                $list[$k]["name"] = !empty($_user[$v["com_uid"]]) ? $_user[$v["com_uid"]] : "" ;
                $list[$k]["com_created"] = formatTimestamp($v["com_created"]);
                $list[$k]["com_modified"] = formatTimestamp($v["com_modified"]);
                $list[$k]["avatar"] = $_avatar[$v["com_uid"]];
            }
            $ret["list"] = $list;
        }
        return $ret;
    }
    
    public function getItemidtoModule($itemid){
        if ( empty($itemid)) return false;
        $criteria = new CriteriaCompo(new Criteria("com_itemid",$itemid));
        $criteria->setLimit(1);
        if ( $list = $this->getAll($criteria,array("com_modid"),false) ) {
            $list = array_values($list);
            $modid = $list["0"]["com_modid"];
            $module_handler = xoops_gethandler("module");
		    $_module_obj = $module_handler->get($modid);
		    return $_module_obj->getVar("dirname");
        }
        return false;        
    }
}